#include "EventPoller.h"
#include "Game.h"
#include "GameOverState.h"
#include "GUIManager.h"
#include "PlayState.h"
#include "SplashState.h"
#include "StringUtil.h"
#include "TextureManager.h"

void GameOverState::LoadButtons()
{
	//THE MENU BUTTON
	m_menuButt.SetTexPath("Textures/MenuButt.png");
	m_menuButt.SetPos(Vec2f(0.5f, 0.2f));
	m_menuButt.SetDims(Vec2f(0.4f, 0.2f));
	m_menuButt.SetActivate(true);

	//THE RESTART BUTTON
	m_restartButt.SetTexPath("Textures/Restart.png");
	m_restartButt.SetPos(Vec2f(1.0f, 0.2f));
	m_restartButt.SetDims(Vec2f(0.4f, 0.2f));
	m_restartButt.SetActivate(true);
}

void GameOverState::LoadText()
{
	//THE TEXT
	m_text.SetTexPath("Textures/Olde English Font Sheet.png");
	m_text.SetPos(Vec2f(-0.7f, -0.2f));
	m_text.SetSize(20);
	m_text.SetString("Your Score:");
	m_text.SetActivate(true);

	//THE SCORE
	m_scoreText.SetTexPath("Textures/Olde English Font Sheet.png");
	m_scoreText.SetPos(Vec2f(0.3f, -0.2f));
	m_scoreText.SetSize(20);
	m_scoreText.SetString(ToString(ThePlayState::Instance()->GetScore()));
	m_scoreText.SetActivate(true);
}

void GameOverState::OnActivated()
{
	TheEventPoller::Instance()->AddHandler(this);

	//THE BACKGROUND IMAGE
	m_bkgroundImg.SetTexPath("Textures/Game Over.png");
	m_bkgroundImg.SetPos(Vec2f(0.0f, 0.0f));
	m_bkgroundImg.SetDims(Vec2f(2.0f, 2.0f));
	m_bkgroundImg.SetActivate(true);

	//THE BUTTONS
	LoadButtons();

	//THE CUSTOM CURSOR
	m_custCursor.SetDims(Vec2f(0.1f, 0.1f));
	m_custCursor.SetActivate(true);

	//THE TEXT
	LoadText();
}

void GameOverState::OnDeactivated()
{
	TheEventPoller::Instance()->RemoveHandler(this);

	m_bkgroundImg.SetActivate(false);
	m_menuButt.SetActivate(false);
	m_restartButt.SetActivate(false);
	m_text.SetActivate(false);
	m_scoreText.SetActivate(false);
}

void GameOverState::Draw()
{
	glClearColor(1,1,1,1);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	TheGUIManager::Instance()->Draw();
}

void GameOverState::Update()
{
	m_menuButt.SetPressed(false);
	m_restartButt.SetPressed(false);
}

void GameOverState::OnKeyboardEvent(const SDL_KeyboardEvent& k)
{
	if(k.keysym.sym == SDLK_ESCAPE && k.state == SDL_PRESSED)
	{
		exit(0);
	}
}

void GameOverState::OnMouseButtonDownEvent(const SDL_MouseButtonEvent& mb)
{
	m_menuButt.OnMouseButtonDownEvent(mb);
	m_restartButt.OnMouseButtonDownEvent(mb);

	if(mb.button == SDL_BUTTON_LEFT)
	{
		if(mb.state == SDL_PRESSED)
		{
			if (m_menuButt.GetPressed())
			{
				TheGame::Instance()->SetGameState(TheSplashState::Instance());
			}
			
			if (m_restartButt.GetPressed())
			{
				TheGame::Instance()->SetGameState(ThePlayState::Instance());
			}
		}
		else if(mb.state == SDL_RELEASED)
		{

		}
	}
}

void GameOverState::OnMouseMotionEvent(const SDL_MouseMotionEvent& mm)
{
	m_menuButt.OnMouseMotionEvent(mm);
	m_restartButt.OnMouseMotionEvent(mm);
	m_custCursor.OnMouseMotionEvent(mm);

	if (m_menuButt.GetOver())
	{
		m_menuButt.SetTexPath("Textures/MenuButt Glow.png");
	}
	else if (m_restartButt.GetOver())
	{
		m_restartButt.SetTexPath("Textures/Restart Glow.png");
	}
	else
	{
		m_menuButt.SetTexPath("Textures/MenuButt.png");
		m_restartButt.SetTexPath("Textures/Restart.png");
	}
}